package org.example.service.impl;

import com.baomidou.mybatisplus.extension.toolkit.Db;
import org.example.domain.po.SonTicket;
import org.example.domain.po.Ticket;
import org.example.domain.vo.SonTicketVO;
import org.example.mapper.SonTicketMapper;
import org.example.service.ISonTicketService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yu
 * @since 2024-07-14
 */
@Service
public class SonTicketServiceImpl extends ServiceImpl<SonTicketMapper, SonTicket> implements ISonTicketService {

    @Override
    public HashMap<Integer, List<SonTicket>> getList() {
        HashMap<Integer, List<SonTicket>> map = new HashMap<>();
        List<SonTicket> sonTicketList= lambdaQuery().list();
        for (SonTicket sonTicket : sonTicketList) {
            List<SonTicket> list;
            if(map.get(sonTicket.getFatherId())!=null){
                list = new ArrayList<>(map.get(sonTicket.getFatherId()));
            }else{
                list = new ArrayList<>();
            }
            list.add(sonTicket);
            map.put(sonTicket.getFatherId(), list);
        }
        return map;
    }

    @Override
    public List<SonTicketVO> selectList(String beginAddress, String endAddress, String date) {
        HashMap<Integer, List<SonTicket>> hashMap=getList();
        List<SonTicketVO> sonTicketVOS=new ArrayList<>();
        for(List<SonTicket> sonTicketList:hashMap.values()){
            SonTicketVO sonTicketVO=new SonTicketVO();
            int flag=0;
            for(SonTicket sonTicket:sonTicketList){
                if(flag==1&&!Objects.equals(sonTicket.getNextAddress(), endAddress)){
                    sonTicketVO.setPrice(sonTicketVO.getPrice().add(sonTicket.getPrice()));
                }
                if(flag==0&&Objects.equals(sonTicket.getNowAddress(), beginAddress) && Objects.equals(sonTicket.getBeginTime().substring(0,10), date)){
                    Ticket ticket= (Db.lambdaQuery(Ticket.class)
                                    .eq(Ticket::getId,sonTicket.getFatherId())
                                            .select(Ticket::getNumber)
                                                    .one());
                    sonTicketVO.setNumber(ticket.getNumber());
                    sonTicketVO.setBeginTime(sonTicket.getBeginTime());
                    sonTicketVO.setNowAddress(sonTicket.getNowAddress());
                    sonTicketVO.setPrice(sonTicket.getPrice());
                    flag=1;
                }if(flag==1&&Objects.equals(sonTicket.getNextAddress(), endAddress)){
                    sonTicketVO.setNextAddress(sonTicket.getNextAddress());
                    sonTicketVO.setNextAddress(sonTicket.getNextAddress());
                    sonTicketVO.setEndTime(sonTicket.getEndTime());
                    sonTicketVO.setPrice(sonTicketVO.getPrice().add(sonTicket.getPrice()));
                    flag=2;
                    break;
                }
            }
            if(flag==2)sonTicketVOS.add(sonTicketVO);
        }
        return sonTicketVOS;
    }

}
